package ybt.vueelement.controller;


import java.util.HashSet;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import ybt.vueelement.custom.AjaxResult;
import ybt.vueelement.custom.TablePageable;
import ybt.vueelement.entity.Course;
import ybt.vueelement.form.CourseForm;
import ybt.vueelement.service.CourseService;


@RestController
@RequestMapping(value = "/course")
@CrossOrigin
public class CourseController {
	@Autowired
	private CourseService service;
	
	  @RequestMapping(value = "/courselist")	  
	  @PreAuthorize(value = "hasRole('ROLE_ADMIN')") 
	  public Object list() {
		  List<Course> list = service.findAll(); 
		  return new AjaxResult(list); 
	}
	@RequestMapping(value = "/page")
	public Object page(TablePageable tablePageable,CourseForm form) {
		PageRequest pageRequest = tablePageable.buildPageRequest();
		Specification<Course> spec = buildSpec(form);
		Page<Course> page = service.findAll(spec, pageRequest);
		return new AjaxResult(AjaxResult.buildTableResult(page)) ;
	}
	
	
	
	
	  @SuppressWarnings("serial")
	  private Specification<Course> buildSpec(CourseForm form)  {
		Specification<Course> spec=new Specification<Course>() {

			@Override
			public Predicate toPredicate(Root<Course> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				String search = form.getSearch();
				if(StringUtils.hasText(search)) {					
					  HashSet<Predicate> rules2=new HashSet<>();
					  rules2.add(cb.like(root.get("classHour").as(String.class),"%"+search+"%"));
					  rules2.add(cb.like(root.get("name"),"%"+search+"%"));
					  
					  rules.add(cb.or(rules2.toArray( new Predicate[rules2.size()])));
					  
				}
					return cb.and(rules.toArray(new Predicate[rules.size()]));
				}			
		};
		return spec;
	}
	  
	
	@RequestMapping(value = "/save")
	public Object save(Course form) {
		Course model=new Course();
		Integer id = form.getId();
		if(id!=null) {
			model=service.findById(id);
		}
		BeanUtils.copyProperties(form, model,"id");
		Course result = service.save(model);
		return new AjaxResult(result);
	}
	
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Object delete(Integer id) {
		try {
			service.deleteById(id);
			return new AjaxResult("数据删除成功");
		}catch (Exception e) {
			return new AjaxResult(false,"数据删除失败");
		}
	}
}
	